<!--
 - top_scores.php
 - This form retrieve and display top 25 scores record from the MySql database. 
 - 
 - 
 - Revision History: 
 -  
 - Date     Author       Version   Changes
 - ======== ============ ========= ================================================================
 - 03/21/10 Steve Hu     1.0       Initial version.
 - 04/02/10 Steve Hu	 3.0	   Redesigned the website using css and images.
 -								   Added Quick Stats section and created Quick stats query.
 - ======== ============ ========= ================================================================
 -
 -->
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" type="text/css" href="css/stylesheet.css" media="screen">
	<title>Sigma - Top 100 Scores</title>
</head>
<?
	/* Setup database connection.  */
	include("./sigma_dataconn.php");
	
	date_default_timezone_set('America/Vancouver');
	$today = date("F j, Y"); 
	
	/* Retrieve data from score_data table and sort descanding by score.  */
	$sql = "SELECT  * FROM score_data where Score > 0 ORDER BY Score DESC LIMIT 100";
	$result = mysql_query($sql, $connection) or die( "Could not execut sql: $sql");
	$num_result = mysql_num_rows($result);
	
	/* Retrieving highest score and best player.  */
	$sql = "SELECT * FROM score_data WHERE score=(SELECT MAX(score) FROM score_data)";
	$bestresult = mysql_query($sql, $connection) or die( "Could not execut sql: $sql");

	$row = mysql_fetch_array($bestresult);
	$bestScore = $row["Score"] ;
	$bestPlayer = $row["UserName"];
	
	/* Retrieving average score and count number of player.  */
	$sql = "SELECT COUNT(Score) AS numRecords, AVG(Score) as avgScore FROM score_data";
	$summaryresult = mysql_query($sql, $connection) or die( "Could not execut sql: $sql");

	$row = mysql_fetch_array($summaryresult);
	$avgScore = $row["avgScore"] ;
	$numRecords = $row["numRecords"];

	/* Retrieving weekly top score & Player.  */
	$sql = "SELECT * FROM score_data WHERE score=(SELECT MAX(score) FROM score_data WHERE YEARweek(Date) = YEARweek(CURDATE()))";
	$weeklyresult = mysql_query($sql, $connection) or die( "Could not execut sql: $sql");

	$row = mysql_fetch_array($weeklyresult);
	$weeklyScore = $row["Score"] ;
	$weeklyPlayer = $row["UserName"] ;
	
	/* Check if there is any player this week.  */
	if (strlen($weeklyPlayer) == 0)
	{
		$weeklyPlayer = "None";
		$weeklyScore = 0;
	}
	/* Terminate SQL Connection.  */
	mysql_close($connection);
?>
<body>
	<div id="layout">
		<div id="header">
			<a href="./top_scores.php">
				<img src="images/sigma_logo.jpg" alt="logo">
			</a>
		</div>

		<div>
			<ul id="menu">
				<li><a href="./top_scores.php" target="_self">Home</a></li>
				<li><a href="http://members.000webhost.com/login.php" target="_blank">Admin Login</a></li>
				<li><a href="http://code.google.com/p/s-i-g-m-a/" target="_blank">About Team DRYSO</a></li>
			</ul>
			<img src="images/bg_menu_bottom.jpg" alt="">
		</div>

		<div id="container-a">
			<div id="left-side">
				<div id="stats">
					<h3>Quick Stats</h3>
					<dl>
					<dt>Current Date</dt>
					<dd><font color="red"><b><? echo $today ?></b></font></dd>
					</dl>	
					<dl>
					<dt>No. 1 Player</dt>
					<dd><b><? echo $bestPlayer ?></b></dd>
					</dl>				
					<dl>
					<dt>Best Score</dt>
					<dd><? echo $bestScore ?></dd>
					</dl>	
					<dl>
					<dt>Weekly Best Player</dt>
					<dd><b><? echo $weeklyPlayer ?></b></dd>
					</dl>	
					<dl>
					<dt>Weekly Best Score</dt>
					<dd><? echo $weeklyScore ?></dd>
					</dl>	
					<dl>
					<dt>Total Records</dt>
					<dd><? echo $numRecords ?></dd>
					</dl>
					<dl>
					<dt>Average Score</dt>
					<dd><? echo round($avgScore) ?></dd>
					</dl>	

				</div> 
			</div>

			<div id="center-page">
				<div id="scores">
					<h3>Top 100 Scores</h3>
					<table border="0" cellpadding="3">
						<tbody>
							<tr>
								<th>Rank</th>
								<th>&nbsp;&nbsp;&nbsp;</th>
								<th>Player</th>
								<th>&nbsp;&nbsp;&nbsp;</th>
								<th>Score</th>
								<th>&nbsp;&nbsp;&nbsp;</th>
								<th>Date Submitted</th>
							</tr>
							<?
								/* Fetch all records and display inside a table.  */
								for ($i=0; $i < $num_result; $i++) 
								{
									$row = mysql_fetch_array($result);

									echo "<tr><td align=\"center\">";
									echo $i+1;
									echo "</td><td>&nbsp;&nbsp;&nbsp;</td><td>";
									echo $row["UserName"];
									echo "</td><td>&nbsp;&nbsp;&nbsp;</td><td align=\"right\">";
									echo $row["Score"];
									echo "</td><td>&nbsp;&nbsp;&nbsp;</td><td align=\"center\">";
									echo date('Y-m-d',strtotime($row["Date"]));
									echo "</td>";
									echo "</tr>";
								}
							?>
						</tbody>
					</table>
					<div class="baktotop">
						<p>
							<a href="#layout">Back to top</a>
						</p>
					</div>
				</div>
			</div>
		</div>            

		<div id="footer">
			<ul>
				<li><a href="./top_scores.php" target="_self">Home</a></li>
				<li><a href="http://members.000webhost.com/login.php" target="_blank">Admin Login</a></li>
				<li><a href="http://code.google.com/p/s-i-g-m-a/" target="_blank">About Team DRYSO</a></li>
			</ul>
			<p>
				Copyright &copy; <a href="mailto:teamdryso@gmail.com">Team DRYSO</a> 2010 
				| Designed by <a href="mailto:steveh@sfu.ca">Steve Hu</a>
			</p>
		</div>
	</div>
</body>
</html>